from flask_sqlalchemy import SQLAlchemy
from database import db
from sqlalchemy.types import BINARY

# class UserModel(db.Model):
    # username = db.Column(db.String(100))
    # password = db.Column(db.String(200), nullable=False)
class User(db.Model):
    __tablename__ = 'user'
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    # 用户id ，作为主键
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(128), unique=True, nullable=False)
    # 存储密码的哈希值
    hash_password = db.Column(db.BINARY(60),nullable=True)
    # 负责与密码哈希值共同生成公私钥对的用户盐值
    user_salt = db.Column(db.BINARY(128),nullable=True)
    # 邮箱必须唯一
    email = db.Column(db.String(100), nullable=False, unique=True) 
    # 这里的datetime.now是一个函数，而datetime.now()是一个值
    from datetime import datetime
    join_time = db.Column(db.DateTime, default=datetime.now)
    

    # 用于在数据库中查询用户
    @classmethod
    def get_by(cls, **kwargs):
        return cls.query.filter_by(**kwargs).first()

# 邮件验证码
class EmailCaptchaModel(db.Model):
    __tablename__ = "email_captcha"
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(100), nullable=False)
    captcha = db.Column(db.String(10), nullable=False)